package com.neusoft.controller;

import com.neusoft.entity.Rent;
import com.neusoft.entity.Role;
import com.neusoft.entity.common.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@Controller
@RequestMapping("roleConsumer")
public class RoleConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    private String url_prefix = "http://localhost:8001";
//      private String url_prefix = "http://provider-neusoft";

    @RequestMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        String url = url_prefix + "/role/queryById/" + id;
        return restTemplate.getForObject(url, Role.class);
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, Role bean) {
        System.out.println("bean值为："+bean);
        System.out.println("page值为："+page);
        System.out.println("limit值为："+limit);
        String url = url_prefix + "/role/queryAll/"+page+"/"+limit;
        return restTemplate.postForObject(url,bean, CommonResult.class);
    }

    @PostMapping("/{ids}")
    @ResponseBody
    public void deleteById(@PathVariable Integer[] ids){
        for(int i=0;i<ids.length;i++) {
            System.out.println("ids值为：" + ids[i]);
        }
        StringBuffer sb = new StringBuffer();
        System.out.println(Arrays.toString(ids));
        sb.append(Arrays.toString(ids));
        sb.deleteCharAt(sb.indexOf("["));
        sb.deleteCharAt(sb.lastIndexOf("]"));
        System.out.println(sb.toString());
        String url = url_prefix+"/role/" + sb.toString();
        restTemplate.delete(url,sb.toString());
    }

    @PostMapping("save")
    @ResponseBody
    public void save(Role bean) {
        String url = url_prefix + "/role/save";
        System.out.println(bean);
        restTemplate.put(url, bean);
    }

    @RequestMapping("toList")
    public String toList() {
        return "menu/role_list";
    }

    @RequestMapping("toAuthList/{id}")
    public String toAuthList(@PathVariable Integer id, Model model) {
        model.addAttribute("role", queryById(id));

        return "menu/role_auth_list";
    }

    @GetMapping("/")
    public String toAdd(Model model) {
        model.addAttribute("bean", new Role());

        return "menu/role_add";
    }

    @GetMapping("/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        System.out.println("id:::" + id);
        Role bean = (Role) queryById(id);
        model.addAttribute("bean", bean);

        return "menu/role_add";
    }
}
